package ripple.test.tools;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ripple.server.RippleServer;

public class RippleServerProcess {

    private static final Logger LOGGER = LoggerFactory.getLogger(RippleServerProcess.class);

    public static String Star_OVERLAY = "tree";
    public static String TREE_OVERLAY = "tree";

    private RippleServer server;
    private Process p;

    public static void main(String[] args) throws Exception {
        String storageLocation;
        int serverId;

        // String storageLocation = System.getProperty("storageLocation");
        // FileWriter fileWriter = new FileWriter("RippleServerProcess.txt");
        // fileWriter.write("RippleServerProcess run\n");
        // fileWriter.close();
        // if (storageLocation == null) {
        //     String defaultStorageLocation = "defaultDatabase";
        //     LOGGER.info("[Main] Missing parameter: storageLocation (-DstorageLocation). Using default: " + defaultStorageLocation);
        //     storageLocation = defaultStorageLocation;
        // }
        // String apiPort = System.getProperty("apiPort");
        // if (apiPort == null) {
        //     LOGGER.info("[Main] Missing parameter: API port (-DapiPort). Using random port.");
        //     apiPort = "0";
        // }
        // String uiPort = System.getProperty("uiPort");
        // if (uiPort == null) {
        //     LOGGER.info("[Main] Missing parameter: UI port (-DuiPort). Using random port.");
        //     uiPort = "0";
        // }
        // String protocol = System.getProperty("protocol");
        // if (protocol == null) {
        //     LOGGER.info("[Main] Missing parameter: protocol (-Dprotocol). Using STAR protocol.");
        //     protocol = "star";
        // }
        // String id = System.getProperty("id");
        // if (id == null) {
        //     LOGGER.info("[Main] Missing parameter: id (-Did).");
        //     return;
        // }
        // String nodes = System.getProperty("nodes");
        // if (nodes == null) {
        //     LOGGER.info("[Main] Missing parameter: nodes (-Dnodes).");
        //     return;
        // }
        // System.out.println("[RippleServerProcess] server id: " + id);
        // System.out.println("[RippleServerProcess] storageLocation: " + storageLocation);
        BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
        while (true) {

            // System.out.println("[RippleServerProcess] while true ");
            // break;
            String strLine = bfr.readLine();
            LOGGER.info("[RippleServerProcess] do while true.");
            LOGGER.info("[RippleServerProcess] strLine: " + strLine);
            if (strLine != null) {
                System.out.println(strLine);//这个地方的输出在子进程控制台是无法输出的，只可以在父进程获取子进程的输出
                // System.out.println("[RippleServerProcess] server id: " + id);
                // System.out.println("[RippleServerProcess] storageLocation: " + storageLocation);
            } else {
                // return;
                break;
            }
        }

        // BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
        // int i = 0;
        // while (true) {
        //     System.out.println("Enter while true, i = " + i);//这个地方的输出在子进程控制台是无法输出的，只可以在父进程获取子进程的输出
        //     fileWriter.write("Enter while true" + "\n");
        //     i++;
        //     // if (i == 0) {
        //     //     System.out.println("Enter while true, i = " + i);//这个地方的输出在子进程控制台是无法输出的，只可以在父进程获取子进程的输出
        //     //     fileWriter.write("Enter while true" + "\n");
        //     // }
        //     String strLine = bfr.readLine();
        //     if (strLine != null) {
        //         System.out.println(strLine);//这个地方的输出在子进程控制台是无法输出的，只可以在父进程获取子进程的输出
        //         fileWriter.write(strLine + "\n");
        //     } else {
        //         // return;
        //         break;
        //     }
        // }
        // fileWriter.close();
        return;
    }

    public RippleServerProcess(int id, String storageLocation, String overlay) {
        try {
            switch (overlay) {
                case "star":
                    ProcessBuilder pb = new ProcessBuilder("java",
                            "@/tmp/cp_u0pc111uhmptm668ezj50qb8.argfile",
                            "ripple.test.ProcessTest");
                    Process p = pb.start();
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
        }

    }

    public boolean stop() {
        return this.server.stop();
    }

}
